HackerRank Larry's Array
提出
code: python
import math
import os
import random
import re
import sys
#
# Complete the 'larrysArray' function below.
#
# The function is expected to return a STRING.
# The function accepts INTEGER_ARRAY A as parameter.
#
def larrysArray(A):
# Write your code here
if __name__ == '__main__':
t = int(input().strip())
for t_itr in range(t):
n = int(input().strip())
A = list(map(int, input().rstrip().split()))
result = larrysArray(A)
fptr.write(result + '\n')
fptr.close()
解説
code: python
import math
import os
import random
import re
import sys
#
# Complete the 'larrysArray' function below.
#
# The function is expected to return a STRING.
# The function accepts INTEGER_ARRAY A as parameter.
#
def larrysArray(A):
# Write your code here
s = 0
# calculate number of inversions
# 1 3 4 2 -> 0 + 1 + 1 + 0
# 1 2 3 5 4 -> 0 + 0 + 0 + 1 + 0
for i in range(n):
for j in range(i+1, n):
s += 1
if s%2 == 0:
return "YES"
else:
return "NO"
if __name__ == '__main__':
t = int(input().strip())
for t_itr in range(t):
n = int(input().strip())
A = list(map(int, input().rstrip().split()))
result = larrysArray(A)
fptr.write(result + '\n')
fptr.close()
メモ
https://www.youtube.com/watch?v=Fq88svB99MA